/obj/item/weapon/cannibis
	name = "Cannibis"
	desc = "cannibis lol"
	icon = 'Cannibis.dmi'
	icon_state = "cannibis"
	w_class = 2.0

/obj/item/weapon/cannibis/attackby(obj/item/weapon/W as obj,mob/user as mob)
	..()
	if(istype(W,/obj/item/weapon/paper))
		user << "You roll the cannibis in the paper."
		var/obj/item/clothing/mask/joint/J = new/obj/item/clothing/mask/joint(get_turf(src))
		del(W)
		del(src)
		user.put_in_hand(J)
	else
		return

/obj/item/clothing/mask/joint
	name = "Cannibis Joint"
	desc = "A joint of cannibis."
	icon = 'Cannibis.dmi'
	icon_state = "joint"
	item_state = "jointoff"
	w_class = 2.0
	var/lit = 0
	var/icon_on = "jointon"
	var/icon_off = "jointoff"
	var/icon_butt = "jointbutt"
	var/lastHolder = null
	var/smoketime = 300
	var/chem_volume = 15
	var/butt_count = 1

/obj/item/clothing/mask/joint/New()
	flags |= NOREACT
	var/datum/reagents/R = new/datum/reagents(15)
	reagents = R
	R.my_atom = src
	reagents.add_reagent("space_drugs",15)
	..()

/obj/item/clothing/mask/joint/Del()
	..()
	del(reagents)

/obj/item/clothing/mask/joint/attackby(obj/item/weapon/W as obj, mob/user as mob)
	..()
	if(istype(W, /obj/item/weapon/weldingtool))
		var/obj/item/weapon/weldingtool/WT = W
		if(WT.isOn())//Badasses dont get blinded while lighting their cig with a welding tool
			light("\red [user] casually lights the [name] with [W], what a badass.")

	else if(istype(W, /obj/item/weapon/lighter/zippo))
		var/obj/item/weapon/lighter/zippo/Z = W
		if(Z.lit > 0)
			light("\red With a single flick of their wrist, [user] smoothly lights their [name] with their [W]. Damn they're cool.")

	else if(istype(W, /obj/item/weapon/lighter))
		var/obj/item/weapon/lighter/L = W
		if(L.lit > 0)
			light("\red After some fiddling, [user] manages to light their [name] with [W].")

	else if(istype(W, /obj/item/weapon/melee/energy/sword))
		var/obj/item/weapon/melee/energy/sword/S = W
		if(S.active)
			light("\red [user] swings their [W], barely missing their nose. They light their [name] in the process.")

	else if(istype(W, /obj/item/weapon/match))
		var/obj/item/weapon/match/M = W
		if(M.lit > 0)
			light("\red [user] lights their [name] with their [W].")
	return

/obj/item/clothing/mask/joint/proc/light(var/flavor_text = "[usr] lights the [name].")
	if(!src.lit)
		src.lit = 1
		src.damtype = "fire"
		if(reagents.get_reagent_amount("plasma")) // the plasma explodes when exposed to fire
			var/datum/effect/effect/system/reagents_explosion/e = new()
			e.set_up(round (src.reagents.get_reagent_amount("plasma")/2.5, 1), get_turf(src), 0, 0)
			e.start()
			del(src)
			return
		if(reagents.get_reagent_amount("fuel")) // the fuel explodes, too, but much less violently
			var/datum/effect/effect/system/reagents_explosion/e = new()
			e.set_up(round (src.reagents.get_reagent_amount("fuel")/5, 1), get_turf(src), 0, 0)
			e.start()
			del(src)
			return
		src.flags &= ~NOREACT // allowing reagents to react after being lit
		src.reagents.handle_reactions()
		src.icon_state = icon_on
		src.item_state = icon_on
		for(var/mob/O in viewers(usr, null))
			O.show_message(flavor_text, 1)
		processing_objects.Add(src)


/obj/item/clothing/mask/joint/process()
	var/turf/location = get_turf(src)
	src.smoketime--
	if(src.smoketime < 1)
		if(ismob(src.loc))
			var/mob/living/M = src.loc
			M << "\red Your [src.name] goes out."
			put_out()
			M.update_clothing()
		else
			put_out()
		processing_objects.Remove(src)
		return
	if(location)
		location.hotspot_expose(700, 5)
	if(reagents && reagents.total_volume)	//	check if it has any reagents at all
		if( iscarbon(src.loc) && (src == loc:wear_mask) ) // if it's in the human/monkey mouth, transfer reagents to the mob
			var/mob/living/carbon/C = loc
			if(prob(15)) // so it's not an instarape in case of acid
				reagents.reaction(C, INGEST)
			reagents.trans_to(C, REAGENTS_METABOLISM)
		else // else just remove some of the reagents
			reagents.remove_any(REAGENTS_METABOLISM)
	return


/obj/item/clothing/mask/joint/dropped(mob/user as mob)
	if(src.lit == 1)
		src.visible_message("\red [user] calmly drops and treads on the lit [src], putting it out instantly.")
		put_out()
	return ..()

/obj/item/clothing/mask/joint/proc/put_out(mob/user as mob)
	if (src.lit == -1)
		return
	src.lit = -1
	src.damtype = "brute"
	src.icon_state = "jointbutt"
	src.item_state = icon_off
	src.desc = "A [src.name] butt."
	src.name = "[src.name] butt"